﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HPIT.RentHouse.IService;
using HPIT.RentHouse.Admin.Models;
using HPIT.RentHouse.Common;
using HPIT.RentHouse.DTO;
using System.IO;
using CodeCarvings.Piczard;
using CodeCarvings.Piczard.Filters.Watermarks;

namespace HPIT.RentHouse.Admin.Controllers
{
    public class HouseController : Controller
    {
        //反转注入
        private IHouseService _HouseService;
        private IRegionService _RegionService;
        private IIdNameService _IdNameService;
        private ICommunityService _ICommunityService;
        public HouseController(IHouseService HouseService, IRegionService RegionService,
            IIdNameService IdNameService, ICommunityService CommunityService)
        {
            _HouseService = HouseService;
            _RegionService = RegionService;
            _IdNameService = IdNameService;
            _ICommunityService = CommunityService;
        }
        /// <summary>
        /// 房源列表(视图)
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 房源列表显示(方法)
        /// </summary>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult GetList(int start, int length, string name)
        {
            int count = 0;
            var list = _HouseService.GetHouseList(name, start, length, ref count);
            PageModel pageModel = new PageModel();
            pageModel.data = list;
            pageModel.recordsTotal = count;
            pageModel.recordsFiltered = count;
            return Json(pageModel);
        }

        /// <summary>
        /// 添加房源(视图)
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            //通过ViewBag传值把数据字典集合、房源附属设施的数据传递到View视图中

            //获取区域
            ViewBag.RegionList = _RegionService.GetRegionList(0).Select(e => new SelectListItem
            {
                Text = e.Name,
                Value = e.Id.ToString()
            });
            //获取房屋类型
            ViewBag.RoomTypeList = _IdNameService.GetIdNameList(IdNameEnum.房屋类型).Select(e => new SelectListItem
            {
                Text = e.Name,
                Value = e.Id.ToString()
            });
            //获取房屋状态
            ViewBag.StatusList = _IdNameService.GetIdNameList(IdNameEnum.房屋状态).Select(e => new SelectListItem
            {
                Text = e.Name,
                Value = e.Id.ToString()
            });
            //获取装修状态
            ViewBag.DecorateStatusList = _IdNameService.GetIdNameList(IdNameEnum.装修状态).Select(e => new SelectListItem
            {
                Text = e.Name,
                Value = e.Id.ToString()
            });
            //获取户型
            ViewBag.TypeList = _IdNameService.GetIdNameList(IdNameEnum.户型).Select(e => new SelectListItem
            {
                Text = e.Name,
                Value = e.Id.ToString()
            });

            var AttachmentList = _HouseService.GetAttachmentList();
            ViewBag.AttachmentList = AttachmentList;
            return View();
        }

        /// <summary>
        /// 添加房源
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        //取消敏感字符验证
        [ValidateInput(false)]
        public ActionResult AddHouseList(HouseAddDTO dto)
        {
            var result = _HouseService.AddHouse(dto);
            return Json(result);
        }   
        /// <summary>
        /// 上传图片(视图)
        /// </summary>
        /// <param name="houseId"></param>
        /// <returns></returns>
        public ActionResult UploadPicView(long houseId)
        {
            ViewBag.houseId = houseId;
            return View();
        }
        /// <summary>
        /// 上传图片(方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadPic(int houseId, HttpPostedFileBase file)
        {          
            //给上传的图片生成唯一id(文件名称)
            string md5 = CommonHelper.CalcMD5(file.InputStream);
            //上传图片的后缀名
            string ext = Path.GetExtension(file.FileName);
            //上传图片存放的路径
            string path = "/upload/house/" + DateTime.Now.ToString("yyyy/MM/dd") + "/" + md5 + ext;
            //上传图片的缩略图路径
            string thumbPath = "/upload/house/" + DateTime.Now.ToString("yyyy/MM/dd") + "/" + md5 + "_thumb" + ext;
            //图片的物理路径
            string fullPath = HttpContext.Server.MapPath("~" + path);//d://22/upload/2017/07/07/afadsfa.jpg
            string thumbFullPath = HttpContext.Server.MapPath("~" + thumbPath);
            new FileInfo(fullPath).Directory.Create();//尝试创建可能不存在的文件夹

            file.InputStream.Position = 0;//指针复位
                                          //file.SaveAs(fullPath);//SaveAs("d:/1.jpg");
                                          //缩略图
            ImageProcessingJob jobThumb = new ImageProcessingJob();
            jobThumb.Filters.Add(new FixedResizeConstraint(200, 200));//缩略图尺寸200*200
            jobThumb.SaveProcessedImageToFileSystem(file.InputStream, thumbFullPath);

            file.InputStream.Position = 0;//指针复位

            //水印
            ImageWatermark imgWatermark = new ImageWatermark(HttpContext.Server.MapPath("~/images/watermark.jpg"));
            imgWatermark.ContentAlignment = System.Drawing.ContentAlignment.BottomRight;//水印位置
            imgWatermark.Alpha = 50;//透明度，需要水印图片是背景透明的png图片
            ImageProcessingJob jobNormal = new ImageProcessingJob();
            jobNormal.Filters.Add(imgWatermark);//添加水印
            jobNormal.Filters.Add(new FixedResizeConstraint(600, 600));
            jobNormal.SaveProcessedImageToFileSystem(file.InputStream, fullPath);

            //保存到数据库
            var result = _HouseService.AddPic(new HousePicDTO
            {
                HouseId = houseId,
                Url = path,
                ThumbUrl = thumbPath
            });
            return Json(result);         
        }
        /// <summary>
        /// 查看图片(方法+视图)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult LookPicture(long lookid)
        { 
           var list=_HouseService.GetHousePics(lookid); 
            return View(list);  
        }
        /// <summary>
        /// 批量删除图片
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteBatchPics(List<long> picId)
        {
            var result = _HouseService.DeleteBatchPics(picId);
            return Json(result);
        }
        /// <summary>
        /// 根据区Id获取小区信息
        /// </summary>
        /// <param name="regionId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetCommunityList(long regionId)
        {
            var list = _ICommunityService.GetCommuities(regionId);
            return Json(list);
        }
        /// <summary>
        /// 删除房源(方法)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(long id)
        {
            var result = _HouseService.Delete(id);
            return Json(result);
        }
        /// <summary>
        /// 批量删除房源
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteBatch(List<long> ids)
        {
            var result = _HouseService.DeleteBatch(ids);
            return Json(result);
        }
    }
}